package com.example.smartcitydemo.ui.subway.subway.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.chad.library.adapter.base.animation.SlideInRightAnimation
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.LineDataBean
import com.example.smartcitydemo.bean.StationDataBean
import com.example.smartcitydemo.databinding.FragmentSubwayLineBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ui.subway.LeftRecyclerAdapter
import com.example.smartcitydemo.ui.subway.RightRecyclerAdapter
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWayDetailTransform
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWaySharedViewModel
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWayViewModel


/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.subway.subway
 * @NAME: Caleb.F
 * @DATE: 2021/11/16 8:14
 * @Email: 1919796283@qq.com
 */
class SubWayLineFragment : BaseFragment<FragmentSubwayLineBinding>(R.layout.fragment_subway_line) {
    private val subwayViewModel: SubWayViewModel by viewModels()
    private val subWaySharedViewModel: SubWaySharedViewModel by activityViewModels()

    private var lastCheckedId: Int = -1
    private val leftRecyclerAdapter: LeftRecyclerAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        LeftRecyclerAdapter().apply {
            init()
            setDiffCallback(LineDataBean.diffCallback)
            val selectedColor = resources.getColor(R.color.listSelected)
            val unSelectedColor = resources.getColor(R.color.listUnSelected)
            setOnItemClickListener { adapter, view, position ->
                val dataPos = view.tag as Int
                subwayViewModel.getStationInfo(dataPos)
                view.setBackgroundColor(selectedColor)
                if (lastCheckedId != -1) {
                    val viewBack = getViewByPosition(lastCheckedId, R.id.left_background)
                    viewBack?.setBackgroundColor(unSelectedColor)
                }
                lastCheckedId = position
                Log.d(TAG, ":$lastCheckedId ")
            }
        }
    }

    private val rightRecyclerAdapter: RightRecyclerAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        RightRecyclerAdapter().apply {
            init()
            adapterAnimation = SlideInRightAnimation()
            setDiffCallback(StationDataBean)
            setOnItemClickListener { adapter, view, pos ->
                val item = this.getItem(pos)
                val string = item.lines.map {
                    "${it.lineId}"
                }.joinToString(",")
                val subWayTrans = SubWayDetailTransform(item.name, string)
                subWaySharedViewModel.getSubWayDetail(subWayTrans)
                this@SubWayLineFragment.findNavController().navigate(R.id.action_navigation_subway_line_to_navigation_subway_detail)
            }
        }
    }


    override fun initData() {
        subwayViewModel.getLineInfo()
        //默认展示所有的线路信息
        subwayViewModel.getStationFull()
    }

    override fun initView(bindView: FragmentSubwayLineBinding, savedInstanceState: Bundle?) {
        bindView.run {
            toolbarSubwayLine.tvContent.text = "地铁线路"
            leftRecycler.adapter = leftRecyclerAdapter
            rightRecycler.adapter = rightRecyclerAdapter
        }
    }

    override fun addObservers(bindView: FragmentSubwayLineBinding) {
        subwayViewModel.lineInfo.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success ->
                    leftRecyclerAdapter.setDiffNewData(it.data!!.toMutableList())
                is Resource.Loading ->
                    rightRecyclerAdapter.setEmptyView(R.layout.loading_layout)
                is Resource.Failure ->
                    rightRecyclerAdapter.setEmptyView(R.layout.error_layout)
            }
        }
        subwayViewModel.stationInfo.observe(viewLifecycleOwner) {
            when (it) {
                is Resource.Success ->
                    rightRecyclerAdapter.setDiffNewData(it.data!!.toMutableList())
                is Resource.Loading ->
                    rightRecyclerAdapter.setEmptyView(R.layout.loading_layout)
                is Resource.Failure ->
                    rightRecyclerAdapter.setEmptyView(R.layout.error_layout)
            }
        }
    }
}